package org.gcu.filestorage.mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.gcu.filestorage.pojo.ApplyRecord;
import org.gcu.filestorage.pojo.SpaceStatus;

import java.util.List;

@Mapper
public interface ApplyInfoMapper {

    @Select("SELECT * FROM apply_record WHERE account=#{account}")
    List<ApplyRecord> queryApplyRecord(String account);
    @Select("SELECT * FROM space_status WHERE account=#{account}")
    SpaceStatus querySpaceStatus(String account);
    @Insert("INSERT INTO apply_record (account, apply_type, apply_status, apply_volume, apply_reason, apply_time)" +
            "VALUES" +
            "(#{account}, #{applyType}, #{applyStatus}, #{applyVolume}, #{applyReason}, #{applyTime})")
    int insertNewApplyRecord(String account, int applyType, int applyStatus, long applyVolume, String applyReason, long applyTime);

    @Update("UPDATE apply_record SET apply_status=#{applyStatus}, approval_desc=#{approvalDesc} WHERE _id=#{id}")
    int updateApplyRecord(int id, int applyStatus, String approvalDesc);

    @Select("SELECT * FROM apply_record WHERE apply_status=#{applyStatus}")
    List<ApplyRecord> queryApplyRecordByStatus(int applyStatus);

    @Insert("INSERT INTO space_status (account, total_volume, used_volume, remain_volume)" +
            "VALUES" +
            "(#{account}, #{totalVolume}, #{usedVolume}, #{remainVolume})")
    int insertNewSpaceStatus(String account, long totalVolume, long usedVolume, long remainVolume);

    @Update("UPDATE space_status SET total_volume=total_volume+#{totalVolume}," +
            " used_volume=used_volume+#{usedVolume}," +
            " remain_volume=remain_volume+#{remainVolume}" +
            " WHERE account=#{account}")
    int updateSpaceStatus(String account, long totalVolume, long usedVolume, long remainVolume);
}
